<?php
namespace App\Service;

use Illuminate\Support\Facades\DB;

class BaseService
{
    //基础服务

    protected $db; //表对象

    //管理员表
    protected $dbAdmin = 'admin';
    protected $dbAdmLogin = 'admin_login';
    //用户表
    protected $dbUser = 'user'; //用户
    protected $dbUlogin = 'user_login'; //用户登录
    protected $dbYzm = 'user_yzm'; //验证码
    protected $dbUfollow = 'user_follow'; //关注
    protected $dbUtixian = 'user_tixian'; //提现
    protected $dbUmsg = 'user_msg'; //消息提醒
    protected $dbDoctor = 'user_doctor'; //医护资料
    protected $dbPatient = 'user_patient'; //病历卡明细
    protected $dbPatImg = 'user_pat_img'; //病历卡图片
    protected $dbUserve = 'uset_serve'; //服务设置
    protected $dbUyiyuan = 'uset_yiyuan'; //医院设置
    protected $dbUaddress = 'user_address'; //医护收货地址
    //配置
    protected $dbYiyuan = 'hospital'; //医院机构
    protected $dbCate = 'cate'; //分类
    protected $dbImg = 'image'; //图库
    //protected $dbUdSeek = 'ud_seek'; //咨询价目配置
    protected $dbConfig = 'config'; //配置
    protected $dbUdsPatient = 'uds_patient'; //咨询病人
    protected $dbUdsp_cnt = 'udsp_cnt'; //咨询内容
    //贴吧
    protected $dbTieba = 'tieba'; //帖子
    protected $dbTbPay = 'tieba_pay'; //帖子付费阅读
    protected $dbTbLike = 'tieba_like'; //帖子点赞
    protected $dbTbReply = 'tieba_reply'; //帖子回复
    //代办+医护+陪诊
    protected $dbDaiban = 'gc_daiban'; //代办单子
    protected $dbDbAttr = 'gc_db_attr'; //代办属性
    protected $dbYihu = 'gc_yihu'; //小区医护单子
    protected $dbYhAttr = 'gc_yh_attr'; //小区医护属性
    protected $dbPeizhen = 'gc_peizhen'; //陪诊单子
    protected $dbGc = 'gc_square'; //单子集合
    //聊天室
    protected $dbChat = 'chat_room'; //聊天室
    protected $dbChatUid = 'chat_uid'; //成员
    protected $dbChatCnt = 'chat_cnt'; //内容
    protected $dbEmoji = 'chat_emoji'; //表情包
    //药品
    protected $dbDrug = 'drug'; //药物
    protected $dbDrugCate = 'drug_cate'; //药物分类
    protected $dbDrugTag = 'drug_tag'; //药物标签
    protected $dbDrugOrder = 'drug_order'; //药物订单
    //支付
    protected $dbPay = 'pay'; //支付
    protected $dbPnotice = 'pay_notice'; //支付通知

    const YwTypeUdsPatient = 1;   ///1患者咨询医生开启对话uds_patient
    const YwTypeTiebaPay = 2;   ///2患者查看医生帖子tieba_pay
    const YwTypeGcDaiban = 3;   ///3患者发布代办预约订单gc_daiban
    const YwTypeGcYihu = 4;   ///4患者发布小区医护预约订单gc_yihu
    const YwTypeGcPeizhen= 5;   ///5患者发布陪诊预约订单

    /**
     * 分类的类别
     * 1user_doctor的work_type，2user_doctor的work_title，3user_doctor的depart，4user_doctor的seek_type，5tieba的cate_id
     */
    protected $cateTypes = [
        1 => '医护类型', '医护头衔', '医院科室', '医护咨询类型', '帖子分类', '代办服务', '号别类型', '小区医护分类',
    ];
    //性别
    protected $sexs = [
        1 => '男', '女',
    ];
    //医生审核
    protected $doctor_applys = [
        1 => '申请中', '已拒绝', '已通过',
    ];
    /**
     * 小区医护订单流程
     * 1等待接单，2已支付，3医护接单，4病情评估，5上门服务，6服务确认
     */
    protected $yihu_statuss = [
        1 => '新发布', '已支付', '医护接单', '病情评估', '上门服务', '服务确认',
    ];
    /**
     * 陪诊订单流程
     * 1等待接单，2已支付，3抢单成功，4陪诊中，5已完成
     */
    protected $peizhen_statuss = [
        1 => '新发布', '已支付', '抢单成功', '陪诊中', '服务完成',
    ];
    /**
     * 买药订单流程
     * 1新下单，2已支付，3已发货，4已收货
     */
    protected $drug_statuss = [
        1 => '新下单', '已支付', '已发货', '已收货',
    ];

    //返回参数
    public function toback($code = 0, $msg = '', $data = [], $total = 0)
    {
        $msg = $msg ? $msg : 'success';
        return array('code' => $code, 'msg' => $msg, 'data' => $data, 'total' => $total);
    }

    //通过分类名称，获取ID
    public function cateIdByName($cate_name)
    {
        $res = (array) $this->dbConn($this->dbCate)
            ->where('del', 0)
            ->where('title', $cate_name)
            ->first();
        return empty($res) ? 0 : $res['title'];
    }

    //获取分类名称
    public function cateNameById($cateid)
    {
        $res = (array) $this->dbConn($this->dbCate)
            ->where('id', $cateid)
            ->first();
        return empty($res) ? '' : $res['title'];
    }

    //获取医院名称
    public function hospitalNameById($yyid)
    {
        $res = (array) $this->dbConn($this->dbYiyuan)
            ->where('id', $yyid)
            ->first();
        return empty($res) ? '' : $res['title'];
    }

    //获取医院ID
    public function hospitalIdByName($yyname)
    {
        $res = (array) $this->dbConn($this->dbYiyuan)
            ->where('title', $yyname)
            ->first();
        return empty($res) ? 0 : $res['id'];
    }

    /**
     * 连接数据库
     * @param string $db 表名
     * @param array $ws 查询条件【key=>val】
     */
    public function dbConn($db, $ws=[])
    {
        $db = DB::connection('mysql')->table($db);
        if (!empty($ws)) {
            foreach ($ws as $k => $v) {
                $db = $db->where($k, $v);
            }
        }
        return $db;
    }
}